<template>
	<view>
		<view class="list">
			<block v-for="(item,index) in list" :key="index">
				<view class="detail">
					<view class="top">
						<text class="status tag-success" v-if="item.is_default">
							默认
						</text>
					</view>
					<view class="info">
						<view class="line">{{item.contact}} - {{item.mobile}}</view>
						<view class="line">{{item.area.mergename}},{{item.detail}}</view>
					</view>
					<view class="btn-group">
						<u-button customStyle="margin-right:10rpx;" v-if="is_check" size="small" type="success" text="选择地址" @click="checkAddress(item)"></u-button>
						<u-button customStyle="margin-right:10rpx;" v-if="!item.is_default" size="small" type="error" text="删除地址" @click="deleteAddress(item,index)"></u-button>
						<u-button size="small" type="primary" text="修改地址" @click="editAddress(item)"></u-button>
					</view>
				</view>
			</block>
			<view style="margin-top: 50rpx;">
				<u-button size="large" icon="plus" @click="$navigateTo('buy/address/add')">添加地址</u-button>
			</view>
		</view>
	</view>
</template>

<script>
	import {methods} from '@/utils/methods.js';
	export default {
		data() {
			return {
				customer:'',
				list:[],
				is_check:false
			}
		},
		onLoad(e) {
			this.customer=this.$customer();
			if(!this.customer){
				this.$reLaunch('index/index');
				return;
			}
			this.is_check=e.is_check || false;
		},
		onPullDownRefresh() {
			this.pagelist(true);
		},
		onShow() {
			this.pagelist();
		},
		methods: {
			...methods,
			editAddress:function(item){
				uni.setStorageSync('address',item);
				this.$navigateTo('buy/address/add');
			},
			deleteAddress:function(item,index){
				this.$post("buy/delAddress",{
					address_id:item.id
				},true).then(res=>{
					this.list.splice(index,1);
				})
			},
			checkAddress:function(item){
				uni.setStorageSync('address',item);
				this.$navigateBack();
			},
			pagelist:function(pulldown=false){
				this.$get("buy/address",{
					customer_id:this.customer.id
				},true).then(res=>{
					if(pulldown){
						uni.stopPullDownRefresh();
					}		
					this.list=res;	
				});		
			}
		}
	}
</script>

<style lang="scss">
.list{
	padding: 20rpx;
	font-size: 24rpx;
	.detail{
		min-height: 100rpx;
		margin-bottom: 20rpx;
		border-radius: 6rpx;
		box-shadow: 0rpx 0rpx 20rpx #bebebe;
		padding: 20rpx;
		.btn-group{
			margin-top: 20rpx;
			display: flex;
			justify-content: flex-end;
			margin-left: 80%;
			width: 20%;			
		}
		.info{
			margin-top: 20rpx;
			background-color: #f7f7f7;
			padding:10rpx;
			.line{
				height: 50rpx;
				line-height: 50rpx;
			}
		}
	}
}
</style>
